import React, { ReactElement, useState, useRef } from 'react';
import { Button, Tooltip, Tag, Space, Table, message, Popconfirm } from 'antd';
import {
  DownOutlined,
  QuestionCircleOutlined,
  EllipsisOutlined,
  DeleteOutlined,
  SettingOutlined,
} from '@ant-design/icons';
import type { ProColumns, ActionType } from '@ant-design/pro-table';
import ProTable from '@ant-design/pro-table';
import { delServiceType, getServiceType } from '@/api/service-type';

import AddServe from './component/AddServe';
import UpdateServe from './component/UpdateServe';

interface Props {}

export default function index({}: Props): ReactElement {
  type Status = {
    color: string;
    text: string;
  };

  type TableListItem = {
    key: number;
    name: string;
    containers: number;
    creator: string;
    status: Status;
    createdAt: number;
  };

  const columns: ProColumns<TableListItem>[] = [
    {
      title: '名称',
      dataIndex: 'title',
    },
    {
      title: '操作',
      key: 'option',
      valueType: 'option',
      width: 150,
      render: (text, record) => [
        <SettingOutlined
          onClick={() => change(record)}
          key="editable"
          style={{ color: '#4A9FF5' }}
        />,
        <Popconfirm
          key="confirm"
          title="确定删除此条数据吗？"
          onConfirm={() => confirm(record)}
          onCancel={cancel}
          okText="确定"
          cancelText="取消"
        >
          <DeleteOutlined key="del" style={{ color: 'red' }} />
        </Popconfirm>,
      ],
    },
  ];

  const [record, setRecord] = useState({});
  const [visible, setVisible] = useState<boolean>(false);
  //打开抽屉
  const change = (item: any) => {
    // console.log(item, 'item');

    setRecord(item);
    setVisible(true);
  };

  //关闭抽屉
  const close = () => {
    setRecord({});
    setVisible(false);
  };
  const actionRef = useRef<ActionType>();
  const reload = () => {
    actionRef.current?.reload();
  };

  //批量删除
  const delServe = (val: any) => {
    // console.log(val.selectedRowKeys, 'id');
    for (let i = 0; i < val.selectedRowKeys.length; i++) {
      del(val.selectedRowKeys[i]);
    }
  };
  //删除
  const del = async (id: string) => {
    const res = await delServiceType(id);
    // console.log(res, 'res');
    if (res.status == 200) {
      message.success('删除成功');
      reload();
    } else {
      message.error('删除失败');
    }
  };
  function cancel(e: any) {
    // console.log(e);
    message.error('取消删除');
  }
  const confirm = (e: any) => {
    // console.log(e);
    del(e.id);
    // console.log(res);
    // message.success('删除成功');
  };
  return (
    <div>
      <UpdateServe
        reload={reload}
        record={record}
        visible={visible}
        close={close}
      ></UpdateServe>
      <ProTable<TableListItem>
        columns={columns}
        actionRef={actionRef}
        request={async (params = {}, sorter, filter) => {
          // 表单搜索项会从 params 传入，传递给后端接口。
          // console.log(params);
          const { title } = params;
          const res = await getServiceType({ name: title });
          const data = res.data.rows.children;
          return {
            data,
            success: true,
            total: res.data.total,
          };
        }}
        rowSelection={{
          // 自定义选择项参考: https://ant.design/components/table-cn/#components-table-demo-row-selection-custom
          // 注释该行则默认不显示下拉选项
          selections: [Table.SELECTION_ALL, Table.SELECTION_INVERT],
        }}
        // rowKey=

        pagination={{
          showQuickJumper: true,
        }}
        // expandable={{ }}
        // search={false}
        dateFormatter="string"
        //表头
        headerTitle={<AddServe reload={reload}></AddServe>}
        options={false}
        // toolBarRender={() => [
        //   <Button key="show">查看日志</Button>,
        //   <Button key="out">
        //     导出数据
        //     <DownOutlined />
        //   </Button>,
        //   <Button key="primary" type="primary">
        //     创建应用
        //   </Button>,
        // ]}
        tableAlertRender={({
          selectedRowKeys,
          selectedRows,
          onCleanSelected,
        }) => {
          // console.log(selectedRowKeys, 'ss');
          // setRoleId([...selectedRowKeys])
          return (
            <Space size={24}>
              <span>
                已选 {selectedRowKeys.length} 项
                {/* <a style={{ marginLeft: 8 }} onClick={onCleanSelected}>
              取消选择
            </a> */}
              </span>
            </Space>
          );
        }}
        tableAlertOptionRender={(val) => {
          // console.log(a);
          return (
            <Space size={16}>
              <Button
                onClick={() => {
                  delServe(val);
                }}
              >
                批量删除
              </Button>
              {/* <Button onClick={() => delAll(a)}>批量删除</Button> */}
            </Space>
          );
        }}
      />
    </div>
  );
}
